//
//  UIView+Extension.m
//  
//
//  Created by Fire on 15/7/27.
//  Copyright (c) 2015年 Fire. All rights reserved.
//

#import "UIView+Extension.h"


static char kDTActionHandlerTapBlockKey;
static char kDTActionHandlerTapGestureKey;
static char kDTActionHandlerLongPressBlockKey;
static char kDTActionHandlerLongPressGestureKey;

@implementation UIView (Extension)


//- (void)setOrigin:(CGPoint)origin{
//    CGRect frame = self.frame;
//    frame.origin = origin;
//    self.frame = frame;
//}
//
//- (CGPoint)origin{
//    return self.frame.origin;
//}
//
//- (CGPoint)bottomLeft{
//    CGFloat x = self.frame.origin.x;
//    CGFloat y = self.frame.origin.y + self.frame.size.height;
//    return CGPointMake(x, y);
//}
//
//- (CGPoint)bottomRight{
//    CGFloat x = self.frame.origin.x + self.frame.size.width;
//    CGFloat y = self.frame.origin.y + self.frame.size.height;
//    return CGPointMake(x, y);
//}
//
//- (CGPoint)topRight{
//    CGFloat x = self.frame.origin.x + self.frame.size.width;
//    CGFloat y = self.frame.origin.y;
//    return CGPointMake(x, y);
//}
//
//
//- (void)setBottom:(CGFloat)bottom{
//    CGRect frame = self.frame;
//    frame.origin.y = bottom - self.frame.size.height;
//    self.frame = frame;
//}
//
//- (CGFloat)bottom{
//    return self.frame.origin.y + self.frame.size.height;
//}
//
//- (void)setRight:(CGFloat)right{
//    CGFloat delta = right - (self.frame.origin.x + self.frame.size.width);
//    CGRect newframe = self.frame;
//    newframe.origin.x += delta ;
//    self.frame = newframe;
//}
//
//- (CGFloat)right{
//    return self.frame.origin.x + self.frame.size.width;
//}
//
//- (void)setX:(CGFloat)x{
//    CGRect frame = self.frame;
//    frame.origin.x = x;
//    self.frame = frame;
//}
//
//- (CGFloat)x{
//    return self.frame.origin.x;
//}
//
//- (void)setY:(CGFloat)y{
//    CGRect frame = self.frame;
//    frame.origin.y = y;
//    self.frame = frame;
//}
//
//- (CGFloat)y{
//    return self.frame.origin.y;
//}
//
//
//- (void)setWidth:(CGFloat)width{
//    CGRect frame = self.frame;
//    frame.size.width = width;
//    self.frame = frame;
//}
//
//- (CGFloat)width{
//    return self.frame.size.width;
//}
//
//- (void)setHeight:(CGFloat)height{
//    CGRect frame = self.frame;
//    frame.size.height = height;
//    self.frame = frame;
//}
//
//- (CGFloat)height{
//    return self.frame.size.height;
//}
//
//
//- (void)setCenterX:(CGFloat)centerX{
//    CGPoint point = self.center;
//    point.x = centerX;
//    self.center = point;
//}
//
//- (CGFloat)centerX{
//    return self.center.x;
//}
//
//- (void)setCenterY:(CGFloat)centerY{
//    CGPoint point = self.center;
//    point.y = centerY;
//    self.center = point;
//}
//
//- (CGFloat)centerY{
//    return self.center.y;
//}
//
//- (void)setSize:(CGSize)size{
//    CGRect frame = self.frame;
//    frame.size = size;
//    self.frame = frame;
//}
//
//
//- (CGSize)size{
//    return self.frame.size;
//}

+ (instancetype)viewFromeXib{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

+ (instancetype)viewFromBgColor:(UIColor *)bgColor
{
    UIView *view = [UIView new];
    view.backgroundColor = bgColor;
    return view;
}

- (UIImage *)imageWithColors:(NSArray *)colors {
    [self addGradualLayerWithColors:colors];
    return [self convertToImage];
}

- (void)addGradualLayerWithColors:(NSArray *)colors {
    if (colors == nil && colors.count == 0) return;
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = colors;
    gradientLayer.locations = @[@0, @1.0];
    gradientLayer.startPoint = CGPointMake(0.02, 0.5);
    gradientLayer.endPoint = CGPointMake(1.0, 0.5);
    gradientLayer.frame = self.bounds;
    [self.layer addSublayer:gradientLayer];
}

- (UIImage *)convertToImage {
    CGSize s = self.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)setTapActionWithBlock:(void (^)(void))block
{
    UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerTapGestureKey);
    if (!gesture) {
        gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)];
        [self addGestureRecognizer:gesture];
        
        objc_setAssociatedObject(self, &kDTActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    
    objc_setAssociatedObject(self, &kDTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
}

- (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        void(^action)(void) = objc_getAssociatedObject(self, &kDTActionHandlerTapBlockKey);
        if (action) {
            action();
        }
    }
}

- (void)setLongPressActionWithBlock:(void (^)(void))block
{
    UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerLongPressGestureKey);
    if (!gesture) {
        gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForLongPressGesture:)];
        [self addGestureRecognizer:gesture];
        
        objc_setAssociatedObject(self, &kDTActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    
    objc_setAssociatedObject(self, &kDTActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
}

- (void)__handleActionForLongPressGesture:(UITapGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        void(^action)(void) = objc_getAssociatedObject(self, &kDTActionHandlerLongPressBlockKey);
        if (action) {
            action();
        }
    }
}

- (void)removeAllSubviews {
    while (self.subviews.count) {
        UIView* child = self.subviews.lastObject;
        [child removeFromSuperview];
    }
}

@end
